#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include "yaml-cpp/yaml.h"
#include <vector>

const std::string DB_CONF = "config.yaml";
int main(int argc, char * argv[]) {


    /* read  */

    YAML::Node conf = YAML::LoadFile(DB_CONF);
    
    /*----------------------------------- display db_config ----------------------------------------*/
    std::cout << "Database:"<< std::endl;
    std::cout << "domain:  " << conf["custom_db"]["db_domain"].as<std::string>() << std::endl;
    std::cout << "username:" << conf["custom_db"]["db_username"].as<std::string>() << std::endl;
    std::cout << "passwd:  " << conf["custom_db"]["db_passwd"].as<std::string>() << std::endl;
    std::cout << "schema:  " << conf["custom_db"]["db_schema"].as<std::string>() << std::endl;

    /*----------------------------------- display redis ----------------------------------------*/
    std::cout << "Redis" << std::endl;
    std::cout << "redis_domain: " << conf["redis"]["redis_domain"].as<std::string>() << std::endl;
    std::cout << "redis_passwd: " << conf["redis"]["redis_passwd"].as<std::string>() << std::endl;
    
    /*----------------------------------- display hello ----------------------------------------*/
    std::cout << "HelloServer" << std::endl;
    
    /* vector of name string. */
    std::vector<std::string> name_vec = conf["hello"]["num_config"].as<std::vector<std::string> >();
    if(!name_vec.empty())
      std::cout << name_vec[0] << std::endl;

    // write
    {
	    //YAML::Node conf = YAML::Load("");
	    std::ofstream fout("testconfig.xml");
	    conf["lon"]=99.0;
	    conf["lat"]=88.0;
	    conf["geo"]["lon"]=88.8888;
	    conf["geo"]["lat"]=99.8888;

      if (conf["geo"]["lat"]){
        double test_ = conf["geo"]["lat"].as<double>();
        std::cout << test_ << std::endl; 
       }else{

        std::cout << "not fond !!!" << std::endl; 
       }



	    fout <<conf;
	    fout.close();

    }
    return 0;
}

